//
//  NSString+CLEncryption.m
//  CLBasicsDemo
//
//  Created by 小草毋正 on 2017/9/13.
//  Copyright © 2017年 zbw. All rights reserved.
//

#import "NSString+CLEncryption.h"

#import<CommonCrypto/CommonDigest.h>

@implementation NSString (CLEncryption)

+ (NSString *)md5:(NSString *)string {
    return [string md5];
}

- (NSString *)md5 {
    const char *cStr = [self UTF8String];
    unsigned char digest[CC_MD5_DIGEST_LENGTH];
    CC_MD5( cStr, (unsigned)strlen(cStr), digest ); // This is the md5 call
    NSMutableString *output = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH * 2];
    for(int i = 0; i < CC_MD5_DIGEST_LENGTH; i++){
        [output appendFormat:@"%02x", digest[i]];
    }
    return output;
}

+ (NSString *)base64:(NSString *)string {
    return [string base64];
}

- (NSString *)base64 {
    if ((self == nil) || (self == NULL)) {
        return nil;
    } else if (![self isKindOfClass:[NSString class]]) {
        return nil;
    }
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    return [data base64EncodedStringWithOptions:0];
}

+ (NSString *)base64Decode:(NSString *)string {
    return [string base64Decode];
}

- (NSString *)base64Decode {
    if ((self == nil) || (self == NULL)) {
        return nil;
    } else if (![self isKindOfClass:[NSString class]]) {
        return nil;
    }
    NSData *data = [[NSData alloc]initWithBase64EncodedString:self options:0];
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}





@end


